package org.jdemo.tools.gitignore;

import org.jdemo.util.IOUtil;

import java.io.File;
import java.io.FilenameFilter;

/**
 * ClearGitignore
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 2014/10/22
 * @since V1.0
 */
public class ClearGitignore {
    public static void main(String[] args) throws Exception {
        recursion("D:/myspace/space_demo/jdemo");
    }

    public static void recursion(String path) throws Exception{
        if (!new File(path).exists()) {
            return;
        }
        File[] files = new File(path).listFiles();
        for (File file : files) {
            if (file.getName().toLowerCase().equals(".git")) {
                continue;
            }
            if (file.isDirectory()) {
                File[] gitignoreFiles = file.listFiles(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return name.toLowerCase().equals(".gitignore");
                    }
                });
                for (File gitignoreFile : gitignoreFiles) {
                    deleteFile(gitignoreFile);
                }
                recursion(file.getAbsolutePath());
            } else if (file.getName().equalsIgnoreCase(".gitignore")) {
                deleteFile(file);
            }
        }
    }

    public static void deleteFile(File file) {
        IOUtil.deleteFile(file);
        System.out.println("delete:" + file.getAbsolutePath());
    }
}
